﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class ProductRepository : Data.Repository.IProductRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region IProductRepository Members

		/// <summary>
		/// create new product method
		/// </summary>
		/// <param name="theNewProduct"></param>
		/// <returns></returns>
		public int CreateNewProduct(Data.Entity.Product theNewProduct)
		{
			if (theNewProduct == null)
			{
				return 0;
			}

			int result = 0;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_Product_Create(theNewProduct.Name, theNewProduct.ShortDescription, theNewProduct.FullDescription,
					theNewProduct.ShowOnHomePage, theNewProduct.MetaKeywords, theNewProduct.MetaTitle, theNewProduct.AllowUserReviews, theNewProduct.AllowUserRatings,
					theNewProduct.RatingSum, theNewProduct.TotalRatingVotes, theNewProduct.Published, theNewProduct.Deleted,
					theNewProduct.CreatedOn, theNewProduct.UpdatedOn, theNewProduct.Cost, theNewProduct.Size, theNewProduct.SizeUnit,
					theNewProduct.Weight, theNewProduct.WeightUnit, theNewProduct.ListPrice, theNewProduct.Quantity, theNewProduct.Recommend, theNewProduct.LanguageID,
					theNewProduct.ProductCode, theNewProduct.CashTicketLimit, theNewProduct.DisplayOrder);
			}

			return result;
		}

		/// <summary>
		/// delete product method
		/// </summary>
		/// <param name="id">the product's ID</param>
		/// <returns>0: failed 1: successed</returns>
		public int DeleteProduct(int id)
		{
			if (id <= 0)
			{
				return 0;
			}

			int result = 0;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_Product_Delete(id);
			}

			return result;
		}

		public IList<Data.Entity.Product> GetProductsByCategory(int categoryID)
		{
			if (categoryID <= 0)
			{
				return null;
			}

			IList<ECommerce_GetProducts_By_CategoryResult> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = from p in _dbContext.ECommerce_GetProducts_By_Category(categoryID)
						   where !p.Deleted && p.Published
						   select p;

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count() > 0)
			{
				var result = new List<Data.Entity.Product>();

				foreach (var p in lstData)
				{
					var theEntity = new Data.Entity.Product()
					{
						AllowUserRatings = p.AllowUserRatings,
						AllowUserReviews = p.AllowUserReviews,
						Cost = p.Cost,
						CreatedOn = p.CreatedOn,
						Deleted = p.Deleted,
						FullDescription = p.FullDescription,
						ListPrice = p.ListPrice,
						MetaKeywords = p.MetaKeywords,
						MetaTitle = p.MetaTitle,
						Name = p.Name,
						ProductID = p.ProductID,
						Published = p.Published,
						Quantity = p.Quantity,
						RatingSum = p.RatingSum,
						ShortDescription = p.ShortDescription,
						ShowOnHomePage = p.ShowOnHomePage,
						Size = p.Size,
						SizeUnit = p.SizeUnit,
						TotalRatingVotes = p.TotalRatingVotes,
						UpdatedOn = p.UpdatedOn,
						Weight = p.Weight,
						WeightUnit = p.WeightUnit,
						Recommend = p.Recommend,
						LanguageID = p.LanguageID,
						ProductCode = p.ProductCode,
						DisplayOrder = p.DisplayOrder
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.Product> GetProductsByTag(int tagID)
		{
			if (tagID <= 0)
			{
				return null;
			}

			List<ECommerce_GetProducts_By_TagResult> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetProducts_By_Tag(tagID);

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Product>();

				foreach (var p in lstData)
				{
					var theEntity = new Data.Entity.Product()
					{
						AllowUserRatings = p.AllowUserRatings,
						AllowUserReviews = p.AllowUserReviews,
						Cost = p.Cost,
						CreatedOn = p.CreatedOn,
						Deleted = p.Deleted,
						FullDescription = p.FullDescription,
						ListPrice = p.ListPrice,
						MetaKeywords = p.MetaKeywords,
						MetaTitle = p.MetaTitle,
						Name = p.Name,
						ProductID = p.ProductID,
						Published = p.Published,
						Quantity = p.Quantity,
						RatingSum = p.RatingSum,
						ShortDescription = p.ShortDescription,
						ShowOnHomePage = p.ShowOnHomePage,
						Size = p.Size,
						SizeUnit = p.SizeUnit,
						TotalRatingVotes = p.TotalRatingVotes,
						UpdatedOn = p.UpdatedOn,
						Weight = p.Weight,
						WeightUnit = p.WeightUnit,
						Recommend = p.Recommend,
						LanguageID = p.LanguageID,
						ProductCode = p.ProductCode,
						CashTicketLimit = p.CashTicketLimit,
						DisplayOrder = p.DisplayOrder
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public Data.Entity.Product GetProduct(int productID)
		{
			if (productID <= 0)
			{
				return null;
			}

			List<ECommerce_GetProductResult> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetProduct(productID);

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var p = lstData[0];

				Data.Entity.Product result;

				result = new Data.Entity.Product()
				{
					AllowUserRatings = p.AllowUserRatings,
					AllowUserReviews = p.AllowUserReviews,
					Cost = p.Cost,
					CreatedOn = p.CreatedOn,
					Deleted = p.Deleted,
					FullDescription = p.FullDescription,
					ListPrice = p.ListPrice,
					MetaKeywords = p.MetaKeywords,
					MetaTitle = p.MetaTitle,
					Name = p.Name,
					ProductID = p.ProductID,
					Published = p.Published,
					Quantity = p.Quantity,
					RatingSum = p.RatingSum,
					ShortDescription = p.ShortDescription,
					ShowOnHomePage = p.ShowOnHomePage,
					Size = p.Size,
					SizeUnit = p.SizeUnit,
					TotalRatingVotes = p.TotalRatingVotes,
					UpdatedOn = p.UpdatedOn,
					Weight = p.Weight,
					WeightUnit = p.WeightUnit,
					Recommend = p.Recommend,
					LanguageID = p.LanguageID,
					ProductCode = p.ProductCode,
					CashTicketLimit = p.CashTicketLimit,
					DisplayOrder = p.DisplayOrder
				};

				return result;
			}

			return null;
		}

		public int UpdateProduct(Data.Entity.Product theProduct)
		{
			if (theProduct == null || theProduct.ProductID <= 0)
			{
				return 0;
			}

			int result = 0;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbProduct = (from p in _dbContext.Products
								 where p.ProductID == theProduct.ProductID
								 select p).FirstOrDefault();

				if (dbProduct == null)
				{
					result = 0;
				}
				else
				{
					dbProduct.AllowUserRatings = theProduct.AllowUserRatings;
					dbProduct.AllowUserReviews = theProduct.AllowUserReviews;
					dbProduct.Cost = theProduct.Cost;
					dbProduct.CreatedOn = theProduct.CreatedOn;
					dbProduct.Deleted = theProduct.Deleted;
					dbProduct.FullDescription = theProduct.FullDescription;
					dbProduct.ListPrice = theProduct.ListPrice;
					dbProduct.MetaKeywords = theProduct.MetaKeywords;
					dbProduct.MetaTitle = theProduct.MetaTitle;
					dbProduct.Name = theProduct.Name;
					dbProduct.Published = theProduct.Published;
					dbProduct.Quantity = theProduct.Quantity;
					dbProduct.RatingSum = theProduct.RatingSum;
					dbProduct.ShortDescription = theProduct.ShortDescription;
					dbProduct.ShowOnHomePage = theProduct.ShowOnHomePage;
					dbProduct.Size = theProduct.Size;
					dbProduct.SizeUnit = theProduct.SizeUnit;
					dbProduct.TotalRatingVotes = theProduct.TotalRatingVotes;
					dbProduct.UpdatedOn = theProduct.UpdatedOn;
					dbProduct.Weight = theProduct.Weight;
					dbProduct.WeightUnit = theProduct.WeightUnit;
					dbProduct.Recommend = theProduct.Recommend;
					dbProduct.LanguageID = theProduct.LanguageID;
					dbProduct.ProductCode = theProduct.ProductCode;
					dbProduct.CashTicketLimit = theProduct.CashTicketLimit;
					dbProduct.DisplayOrder = theProduct.DisplayOrder;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;
		}

		public IList<Data.Entity.Category> GetTheProductCategories(int productID)
		{
			if (productID <= 0)
			{
				return null;
			}

			List<ECommerce_GetTheProductCategoriesResult> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetTheProductCategories(productID);

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Category>();

				foreach (var c in lstData)
				{
					var theEntity = new Data.Entity.Category()
					{
						CategoryID = c.CategoryID,
						CategoryName = c.CategoryName,
						CreatedOn = c.CreatedOn,
						Deleted = c.Deleted,
						Description = c.Description,
						DisplayOrder = c.DisplayOrder,
						ParentCategoryID = c.ParentCategoryID,
						PictureID = c.PictureID,
						Published = c.Published,
						ShowOnHomePage = c.ShowOnHomePage,
						UpdatedOn = c.UpdatedOn,
						LanguageID = c.LanguageID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// advanced search method
		/// </summary>
		/// <param name="metaKeywords"></param>
		/// <param name="noMetaKeywords"></param>
		/// <param name="categoryID"></param>
		/// <param name="startPrice"></param>
		/// <param name="endPrice"></param>
		/// <returns></returns>
		public IList<Data.Entity.Product> GetProductByFilter(string metaKeywords, string noMetaKeywords, int categoryID, decimal startPrice, decimal endPrice, int languageID)
		{
			List<Data.Product> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				IQueryable<Data.Product> products = null;

				if (categoryID > 0)
				{
					var pids = from prod in _dbContext.ECommerce_GetProducts_By_Category(categoryID)
							   select prod.ProductID;

					products = (from p in _dbContext.Products
								where pids.Contains(p.ProductID)
								select p);
				}
				else
				{
					products = from p in _dbContext.Products
							   select p;
				}

				if (!string.IsNullOrEmpty(metaKeywords))
				{
					products = from p in products
							   where p.MetaKeywords.ToLower().Contains(metaKeywords.ToLower())
							   select p;
				}

				if (!string.IsNullOrEmpty(noMetaKeywords))
				{
					products = from p in products
							   where !p.MetaKeywords.ToLower().Contains(noMetaKeywords.ToLower())
							   select p;
				}

				if (startPrice >= 0)
				{
					products = from p in products
							   where p.ListPrice >= startPrice
							   select p;
				}

				if (endPrice >= 0)
				{
					products = from p in products
							   where p.ListPrice <= endPrice
							   select p;
				}

				var data = from p in products
						   where p.LanguageID.Equals(languageID)
						   select p;

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Product>();

				foreach (var p in lstData)
				{
					var theEntity = new Data.Entity.Product()
					{
						AllowUserRatings = p.AllowUserRatings,
						AllowUserReviews = p.AllowUserReviews,
						Cost = p.Cost,
						CreatedOn = p.CreatedOn,
						Deleted = p.Deleted,
						FullDescription = p.FullDescription,
						ListPrice = p.ListPrice,
						MetaKeywords = p.MetaKeywords,
						MetaTitle = p.MetaTitle,
						Name = p.Name,
						ProductID = p.ProductID,
						Published = p.Published,
						Quantity = p.Quantity,
						RatingSum = p.RatingSum,
						ShortDescription = p.ShortDescription,
						ShowOnHomePage = p.ShowOnHomePage,
						Size = p.Size,
						SizeUnit = p.SizeUnit,
						TotalRatingVotes = p.TotalRatingVotes,
						UpdatedOn = p.UpdatedOn,
						Weight = p.Weight,
						WeightUnit = p.WeightUnit,
						Recommend = p.Recommend,
						LanguageID = p.LanguageID,
						ProductCode = p.ProductCode,
						CashTicketLimit = p.CashTicketLimit,
						DisplayOrder = p.DisplayOrder
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.Product> GetProductsByLanguage(int languageID)
		{
			if (languageID <= 0)
			{
				return null;
			}

			List<Data.Product> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = from p in _dbContext.Products
						   where !p.Deleted && p.Published && p.LanguageID.Equals(languageID)
						   select p;

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Product>();

				foreach (var p in lstData)
				{
					var theEntity = new Data.Entity.Product()
					{
						AllowUserRatings = p.AllowUserRatings,
						AllowUserReviews = p.AllowUserReviews,
						Cost = p.Cost,
						CreatedOn = p.CreatedOn,
						Deleted = p.Deleted,
						FullDescription = p.FullDescription,
						ListPrice = p.ListPrice,
						MetaKeywords = p.MetaKeywords,
						MetaTitle = p.MetaTitle,
						Name = p.Name,
						ProductID = p.ProductID,
						Published = p.Published,
						Quantity = p.Quantity,
						RatingSum = p.RatingSum,
						ShortDescription = p.ShortDescription,
						ShowOnHomePage = p.ShowOnHomePage,
						Size = p.Size,
						SizeUnit = p.SizeUnit,
						TotalRatingVotes = p.TotalRatingVotes,
						UpdatedOn = p.UpdatedOn,
						Weight = p.Weight,
						WeightUnit = p.WeightUnit,
						Recommend = p.Recommend,
						LanguageID = p.LanguageID,
						ProductCode = p.ProductCode,
						CashTicketLimit = p.CashTicketLimit,
						DisplayOrder = p.DisplayOrder
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.Product> GetAllProductsByLanguage(int languageID)
		{
			if (languageID <= 0)
			{
				return null;
			}

			List<Data.Product> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = from p in _dbContext.Products
						   where p.LanguageID.Equals(languageID)
						   select p;

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Product>();

				foreach (var p in lstData)
				{
					var theEntity = new Data.Entity.Product()
					{
						AllowUserRatings = p.AllowUserRatings,
						AllowUserReviews = p.AllowUserReviews,
						Cost = p.Cost,
						CreatedOn = p.CreatedOn,
						Deleted = p.Deleted,
						FullDescription = p.FullDescription,
						ListPrice = p.ListPrice,
						MetaKeywords = p.MetaKeywords,
						MetaTitle = p.MetaTitle,
						Name = p.Name,
						ProductID = p.ProductID,
						Published = p.Published,
						Quantity = p.Quantity,
						RatingSum = p.RatingSum,
						ShortDescription = p.ShortDescription,
						ShowOnHomePage = p.ShowOnHomePage,
						Size = p.Size,
						SizeUnit = p.SizeUnit,
						TotalRatingVotes = p.TotalRatingVotes,
						UpdatedOn = p.UpdatedOn,
						Weight = p.Weight,
						WeightUnit = p.WeightUnit,
						Recommend = p.Recommend,
						LanguageID = p.LanguageID,
						ProductCode = p.ProductCode,
						CashTicketLimit = p.CashTicketLimit,
						DisplayOrder = p.DisplayOrder
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.ProductTag> GetTheProductTags(int theProductID)
		{
			if (theProductID <= 0)
			{
				return null;
			}

			List<Data.ProductTag> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var tagIDs = from t in _dbContext.ProductTag_Product_Mappings
							 where t.ProductID.Equals(theProductID)
							 select t.ProductTagID;

				var data = from tag in _dbContext.ProductTags
						   where tagIDs.Contains(tag.ProductTagID)
						   select tag;

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ProductTag>();

				foreach (var tag in lstData)
				{
					var theEntity = new Data.Entity.ProductTag()
					{
						LanguageID = tag.LanguageID,
						Name = tag.Name,
						ProductCount = tag.ProductCount,
						ProductTagID = tag.ProductTagID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public void SaveProduct_Category_Mapping(Data.Entity.Product_Category_Mapping theProduct_Category_Mapping)
		{
			if (theProduct_Category_Mapping == null)
			{
				return;
			}

			using (LinqToECommerceDBDataContext db = new LinqToECommerceDBDataContext())
			{
				var dbProduct_Category_Mapping = (from pcm in db.Product_Category_Mappings
												  where pcm.CategoryID.Equals(theProduct_Category_Mapping.CategoryID) && pcm.ProductID.Equals(theProduct_Category_Mapping.ProductID)
												  select pcm).FirstOrDefault();

				bool isNew = false;

				if (dbProduct_Category_Mapping == null)
				{
					dbProduct_Category_Mapping = new Product_Category_Mapping();
					isNew = true;
					dbProduct_Category_Mapping.CategoryID = theProduct_Category_Mapping.CategoryID;
					dbProduct_Category_Mapping.ProductID = theProduct_Category_Mapping.ProductID;
				}

				dbProduct_Category_Mapping.DisplayOrder = theProduct_Category_Mapping.DisplayOrder;
				dbProduct_Category_Mapping.IsFeaturedProduct = theProduct_Category_Mapping.IsFeaturedProduct;

				if (isNew)
				{
					db.Product_Category_Mappings.InsertOnSubmit(dbProduct_Category_Mapping);
				}

				db.SubmitChanges();
			}
		}

		public void SaveProductTag_Product_Mapping(Data.Entity.ProductTag_Product_Mapping theProductTag_Product_Mapping)
		{
			if (theProductTag_Product_Mapping == null)
			{
				return;
			}

			using (LinqToECommerceDBDataContext db = new LinqToECommerceDBDataContext())
			{
				var dbProductTag_Product_Mapping = (from ppm in db.ProductTag_Product_Mappings
													where ppm.ProductTagID.Equals(theProductTag_Product_Mapping.ProductTagID) && ppm.ProductID.Equals(theProductTag_Product_Mapping.ProductID)
													select ppm).FirstOrDefault();

				bool isNew = false;

				if (dbProductTag_Product_Mapping == null)
				{
					dbProductTag_Product_Mapping = new ProductTag_Product_Mapping();
					isNew = true;
				}

				dbProductTag_Product_Mapping.ProductID = theProductTag_Product_Mapping.ProductID;
				dbProductTag_Product_Mapping.ProductTagID = theProductTag_Product_Mapping.ProductTagID;

				if (isNew)
				{
					db.ProductTag_Product_Mappings.InsertOnSubmit(dbProductTag_Product_Mapping);
				}

				db.SubmitChanges();
			}
		}

		public IList<Data.Entity.Product_Category_Mapping> GetProduct_Category_MappingsByProduct(int theProductID)
		{
			if (theProductID <= 0)
			{
				return null;
			}

			List<Data.Product_Category_Mapping> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = from pcm in _dbContext.Product_Category_Mappings
						   where pcm.ProductID.Equals(theProductID)
						   orderby pcm.DisplayOrder
						   select pcm;

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Product_Category_Mapping>();

				foreach (var pcm in lstData)
				{
					var theEntity = new Data.Entity.Product_Category_Mapping()
					{
						CategoryID = pcm.CategoryID,
						DisplayOrder = pcm.DisplayOrder,
						IsFeaturedProduct = pcm.IsFeaturedProduct,
						ProductCategoryID = pcm.ProductCategoryID,
						ProductID = pcm.ProductID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public bool DeleteProduct_Category_Mapping(Data.Entity.Product_Category_Mapping theProduct_Category_Mapping)
		{
			if (theProduct_Category_Mapping == null || theProduct_Category_Mapping.ProductCategoryID <= 0)
			{
				return false;
			}

			bool result = false;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbProduct_Category_Mapping = (from pcm in _dbContext.Product_Category_Mappings
												  where pcm.ProductID.Equals(theProduct_Category_Mapping.ProductID) && pcm.CategoryID.Equals(theProduct_Category_Mapping.CategoryID)
												  select pcm).FirstOrDefault();

				if (dbProduct_Category_Mapping == null)
				{
					result = false;
				}
				else
				{
					dbProduct_Category_Mapping.DisplayOrder = theProduct_Category_Mapping.DisplayOrder;
					dbProduct_Category_Mapping.IsFeaturedProduct = theProduct_Category_Mapping.IsFeaturedProduct;

					_dbContext.Product_Category_Mappings.DeleteOnSubmit(dbProduct_Category_Mapping);

					_dbContext.SubmitChanges();

					result = true;
				}
			}

			return result;
		}

		public IList<Data.Entity.ProductTag_Product_Mapping> GetProductTag_Product_MappingByProduct(int theProductID)
		{
			if (theProductID <= 0)
			{
				return null;
			}

			List<Data.ProductTag_Product_Mapping> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ProductTag_Product_Mappings.Where(exp => exp.ProductID == theProductID);

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ProductTag_Product_Mapping>();

				foreach (var ppm in lstData)
				{
					var theEntity = new Data.Entity.ProductTag_Product_Mapping()
					{
						ProductTagID = ppm.ProductTagID,
						ProductID = ppm.ProductID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public bool DeleteProductTag_Product_Mapping(Data.Entity.ProductTag_Product_Mapping theProductTag_Product_Mapping)
		{
			if (theProductTag_Product_Mapping == null)
			{
				return false;
			}

			bool result = false;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbProductTag_Product_Mapping = (from ppm in _dbContext.ProductTag_Product_Mappings
													where ppm.ProductID.Equals(theProductTag_Product_Mapping.ProductID) && ppm.ProductTagID.Equals(theProductTag_Product_Mapping.ProductTagID)
													select ppm).FirstOrDefault();

				if (dbProductTag_Product_Mapping == null)
				{
					result = false;
				}

				_dbContext.ProductTag_Product_Mappings.DeleteOnSubmit(dbProductTag_Product_Mapping);
				_dbContext.SubmitChanges();

				result = true;
			}

			return result;
		}

		public int IsExistingTheProductAndTagMapping(int productID, int tagID)
		{
			if (productID <= 0 || tagID <= 0)
			{
				return -1;
			}

			int result = 0;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				result = (from pt in _dbContext.ProductTag_Product_Mappings
						  where pt.ProductID.Equals(productID) && pt.ProductTagID.Equals(tagID)
						  select pt).Count();
			}

			return result;
		}

		public IList<Data.Entity.Product> GetTopSellingProducts()
		{
			List<ECommerce_GetTopSellingProductsResult> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetTopSellingProducts();

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Product>();

				foreach (var p in lstData)
				{
					var theEntity = new Data.Entity.Product()
					{
						AllowUserRatings = p.AllowUserRatings,
						AllowUserReviews = p.AllowUserReviews,
						Cost = p.Cost,
						CreatedOn = p.CreatedOn,
						Deleted = p.Deleted,
						FullDescription = p.FullDescription,
						ListPrice = p.ListPrice,
						MetaKeywords = p.MetaKeywords,
						MetaTitle = p.MetaTitle,
						Name = p.Name,
						ProductID = p.ProductID,
						Published = p.Published,
						Quantity = p.Quantity,
						RatingSum = p.RatingSum,
						ShortDescription = p.ShortDescription,
						ShowOnHomePage = p.ShowOnHomePage,
						Size = p.Size,
						SizeUnit = p.SizeUnit,
						TotalRatingVotes = p.TotalRatingVotes,
						UpdatedOn = p.UpdatedOn,
						Weight = p.Weight,
						WeightUnit = p.WeightUnit,
						Recommend = p.Recommend,
						LanguageID = p.LanguageID,
						ProductCode = p.ProductCode,
						CashTicketLimit = p.CashTicketLimit,
						DisplayOrder = p.DisplayOrder
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.Product> GetProductsByCategory(int categoryID, int pageIndex, int pageSize, out int totalRecords)
		{
			if (categoryID <= 0)
			{
				totalRecords = 0;

				return null;
			}

			IList<ECommerce_GetProducts_By_CategoryResult> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				totalRecords = (from p in _dbContext.ECommerce_GetProducts_By_Category(categoryID)
								where !p.Deleted && p.Published
								select p).Count();

				var data = (from p in _dbContext.ECommerce_GetProducts_By_Category(categoryID)
							where !p.Deleted && p.Published
							select p).Skip(pageIndex * pageSize).Take(pageSize);

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count() > 0)
			{
				var result = new List<Data.Entity.Product>();

				foreach (var p in lstData)
				{
					var theEntity = new Data.Entity.Product()
					{
						AllowUserRatings = p.AllowUserRatings,
						AllowUserReviews = p.AllowUserReviews,
						Cost = p.Cost,
						CreatedOn = p.CreatedOn,
						Deleted = p.Deleted,
						FullDescription = p.FullDescription,
						ListPrice = p.ListPrice,
						MetaKeywords = p.MetaKeywords,
						MetaTitle = p.MetaTitle,
						Name = p.Name,
						ProductID = p.ProductID,
						Published = p.Published,
						Quantity = p.Quantity,
						RatingSum = p.RatingSum,
						ShortDescription = p.ShortDescription,
						ShowOnHomePage = p.ShowOnHomePage,
						Size = p.Size,
						SizeUnit = p.SizeUnit,
						TotalRatingVotes = p.TotalRatingVotes,
						UpdatedOn = p.UpdatedOn,
						Weight = p.Weight,
						WeightUnit = p.WeightUnit,
						Recommend = p.Recommend,
						LanguageID = p.LanguageID,
						ProductCode = p.ProductCode,
						DisplayOrder = p.DisplayOrder
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.Product> GetProductsByLanguage(int languageID, int pageIndex, int pageSize, out int totalRecords)
		{
			if (languageID <= 0)
			{
				totalRecords = 0;

				return null;
			}

			List<ECommerce_GetProducts_By_LanguageResult> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				totalRecords = (from p in _dbContext.ECommerce_GetProducts_By_Language(languageID)
								where !p.Deleted && p.Published
								select p).Count();

				var data = (from p in _dbContext.ECommerce_GetProducts_By_Language(languageID)
							where !p.Deleted && p.Published
							select p).Skip(pageSize * pageIndex).Take(pageSize);

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Product>();

				foreach (var p in lstData)
				{
					var theEntity = new Data.Entity.Product()
					{
						AllowUserRatings = p.AllowUserRatings,
						AllowUserReviews = p.AllowUserReviews,
						Cost = p.Cost,
						CreatedOn = p.CreatedOn,
						Deleted = p.Deleted,
						FullDescription = p.FullDescription,
						ListPrice = p.ListPrice,
						MetaKeywords = p.MetaKeywords,
						MetaTitle = p.MetaTitle,
						Name = p.Name,
						ProductID = p.ProductID,
						Published = p.Published,
						Quantity = p.Quantity,
						RatingSum = p.RatingSum,
						ShortDescription = p.ShortDescription,
						ShowOnHomePage = p.ShowOnHomePage,
						Size = p.Size,
						SizeUnit = p.SizeUnit,
						TotalRatingVotes = p.TotalRatingVotes,
						UpdatedOn = p.UpdatedOn,
						Weight = p.Weight,
						WeightUnit = p.WeightUnit,
						Recommend = p.Recommend,
						LanguageID = p.LanguageID,
						ProductCode = p.ProductCode,
						CashTicketLimit = p.CashTicketLimit,
						DisplayOrder = p.DisplayOrder
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.Product> GetProductsByTag(int tagID, int pageIndex, int pageSize, out int totalRecords)
		{
			if (tagID <= 0)
			{
				totalRecords = 0;
				return null;
			}

			List<ECommerce_GetProducts_By_TagResult> lstData;

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				totalRecords = (from p in _dbContext.ECommerce_GetProducts_By_Tag(tagID)
								where !p.Deleted && p.Published
								select p).Count();

				var data = (from p in _dbContext.ECommerce_GetProducts_By_Tag(tagID)
							where !p.Deleted && p.Published
							select p);

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Product>();

				foreach (var p in lstData)
				{
					var theEntity = new Data.Entity.Product()
					{
						AllowUserRatings = p.AllowUserRatings,
						AllowUserReviews = p.AllowUserReviews,
						Cost = p.Cost,
						CreatedOn = p.CreatedOn,
						Deleted = p.Deleted,
						FullDescription = p.FullDescription,
						ListPrice = p.ListPrice,
						MetaKeywords = p.MetaKeywords,
						MetaTitle = p.MetaTitle,
						Name = p.Name,
						ProductID = p.ProductID,
						Published = p.Published,
						Quantity = p.Quantity,
						RatingSum = p.RatingSum,
						ShortDescription = p.ShortDescription,
						ShowOnHomePage = p.ShowOnHomePage,
						Size = p.Size,
						SizeUnit = p.SizeUnit,
						TotalRatingVotes = p.TotalRatingVotes,
						UpdatedOn = p.UpdatedOn,
						Weight = p.Weight,
						WeightUnit = p.WeightUnit,
						Recommend = p.Recommend,
						LanguageID = p.LanguageID,
						ProductCode = p.ProductCode,
						CashTicketLimit = p.CashTicketLimit,
						DisplayOrder = p.DisplayOrder
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		#endregion
	}
}
